/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod.reports;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Гплина
 */
public class ReportXMLParser {

    Document doc;
    XPath xpath;

    public ReportXMLParser(String filename){
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            doc = builder.parse(filename);
            XPathFactory factory = XPathFactory.newInstance();
            xpath = factory.newXPath();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ReportXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ReportXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ReportXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Map<String,String> getReports(){
        Map<String,String> res = new HashMap<String, String>();
        try {
            XPathExpression expr = xpath.compile("//report");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                NamedNodeMap atributes = nodes.item(i).getAttributes();
                res.put(atributes.getNamedItem("title").getNodeValue(), atributes.getNamedItem("filename").getNodeValue());
            }
            return res;
        } catch (XPathExpressionException ex) {
            Logger.getLogger(ReportXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public List<ReportParam> getParams(String reportName){
        List<ReportParam> res = new ArrayList<ReportParam>();
        Map<String, String> values = new HashMap<String, String>();
        try {
            XPathExpression expr = xpath.compile("//report[@title='"+reportName+"']/param");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                NamedNodeMap atributes = nodes.item(i).getAttributes();
                String name = atributes.getNamedItem("name").getNodeValue();
                String title = atributes.getNamedItem("title").getNodeValue();

                expr = xpath.compile("//report[@title='"+reportName+"']/param[@title='"+title+"']/variant");
                result = expr.evaluate(doc, XPathConstants.NODESET);
                NodeList variants = (NodeList) result;
                for (int j = 0; j < variants.getLength(); j++){
                    atributes = variants.item(j).getAttributes();
                    values.put(atributes.getNamedItem("title").getNodeValue(), atributes.getNamedItem("value").getNodeValue());
                }
                res.add(new ReportParam(name, title, values));
            }
            return res;
        } catch (XPathExpressionException ex) {
            Logger.getLogger(ReportXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}